#ifndef _ENERMY_H
#define _ENERMY_H

#include"animation.h"
#include"vector2.h"
#include"camera.h"
#include"timer.h"
#include "peabullet.h"

#include<functional>
#include<graphics.h>

extern Atlas atlas_peashooter_idle_right;
extern Camera main_camera;
extern std::vector<Bullet*>bullet_list;

class Enermy
{
public:
	Enermy()
	{
		current_animation.set_atlas(&atlas_peashooter_idle_right);
		current_animation.set_interval(100);
		
		timer_attack_cd.set_wait_time(attack_cd);
		timer_attack_cd.set_callback([&]()
			{
				on_attack();
			});

		size.x = 96,size.y=96;
	}
	~Enermy() = default;

	void set_position(float x, float y)
	{
		position.x = x, position.y = y;
	}

	const Vector2& get_position() const
	{
		return position;
	}

	const Vector2& get_size() const
	{
		return size;
	}

	void on_update(int delta)
	{
		current_animation.on_update(delta);

		timer_attack_cd.on_update(delta);
	}

	void on_draw(const Camera& camera)
	{
		current_animation.on_draw(camera, (int)position.x, (int)position.y);
	}

	void on_attack()
	{
		spawn_pea_bullet(speed_pea);

	}
	void set_pos(int x, int y)
	{
		position.x = x;
		position.y = y;
	}

private:
	Animation current_animation;
	Vector2 size;
	Vector2 position;

	int attack_cd = 750;
	bool can_attack = true;
	Timer timer_attack_cd;
	const float speed_pea = 0.75f;

private:
	void spawn_pea_bullet(float speed)
	{
		PeaBullet* bullet = new PeaBullet();

		Vector2 bullet_position, bullet_velocity;
		const Vector2& bullet_size = bullet->get_size();
		bullet_position.x = position.x + size.x - bullet_size.x / 2;
		bullet_position.y = position.y;
		bullet_velocity.x = speed;
		bullet_velocity.y = 0;

		bullet->set_position(bullet_position.x, bullet_position.y);
		bullet->set_velocity(bullet_velocity.x, bullet_velocity.y);

		bullet_list.push_back(bullet);

	}	
};

#endif
